package org.urbansoft.icl.record;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.urbansoft.icl.field.Field;
import org.urbansoft.icl.field.GenericAField;
import org.urbansoft.icl.field.GenericANField;
import org.urbansoft.icl.field.GenericANSField;
import org.urbansoft.icl.field.GenericNBField;
import org.urbansoft.icl.field.GenericNField;
import org.urbansoft.icl.field.Position;
import org.urbansoft.icl.field.Usage;
import org.urbansoft.icl.field.ValidationCriteria;
import org.urbansoft.icl.field.validation.AuxOnUsValidator;
import org.urbansoft.icl.field.validation.BFDIndicatorValidator;
import org.urbansoft.icl.field.validation.CashLetterDocumentationTypeValidator;
import org.urbansoft.icl.field.validation.OnUsValidator;
import org.urbansoft.icl.field.validation.RecordTypeValidator;
import org.urbansoft.icl.util.RightJustifySpacePadder;

/**
 * The Class CheckDetailRecord.
 */
public class CheckDetailRecord extends Record {
	public Field recordType;
	public Field auxiliaryOnUs;
	public Field externalProcessingCode;
	public Field payorBankRoutingNumber;
	public Field payourBankRoutingNumberCheckDigit;
	public Field onUs;
	public Field itemAmount;
	public Field eceInstitutionItemSequenceNumber;
	public Field documentationTypeIndicator;
	public Field returnAcceptanceIndicator;
	public Field micrValidIndicator;
	public Field bfdIndicator;
	public Field checkDetailRecordAddendumCount;
	public Field correctionIndicator;
	public Field archiveTypeIndicator;
	public Field origin;
	public Field reserved;

	static Logger log = Logger.getLogger(CheckDetailRecord.class);

	/**
	 * Instantiates a new check detail record.
	 */
	public CheckDetailRecord() {
		fields = new ArrayList<Field>();
		recordName = "Check Detail Record";
		recordTypeNumber = "25";
		initFields();
	}

	/**
	 * Instantiates a new check detail record for read purppose.
	 */
	public CheckDetailRecord(String read) {
		// log.info("New CheckDetailRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "Check Detail Record";
		recordTypeNumber = "25";
		initFields2();
	}

	/**
	 * Instantiates a new check detail record for write purpose.
	 */
	public CheckDetailRecord(int read) {
		// log.info("New CheckDetailRecord for WRITE");
		fields = new ArrayList<Field>();
		recordName = "Check Detail Record";
		recordTypeNumber = "25";
		initFields3();
	}

	/**
	 * Inits the fields.
	 */
	private void initFields2() {
		try {
			/* check detail record */
			recordType = new GenericANField(new Position(1, 2), "Record Type");

			/* aux on us */
			auxiliaryOnUs = new GenericANField(new Position(3, 17),
					"Auxiliary On Us");

			/* external processing code */
			externalProcessingCode = new GenericANField(new Position(18, 18),
					"External Processing Code");

			/* payor bank routing number */
			payorBankRoutingNumber = new GenericANField(new Position(19, 26),
					"Payor Bank Routing Number");

			/* payor bank routing number check digit */
			payourBankRoutingNumberCheckDigit = new GenericANField(
					new Position(27, 27),
					"Payor Bank Routing Number Check Digit");
			payourBankRoutingNumberCheckDigit.setUsage(Usage.CONDITIONAL);

			/* on us */
			onUs = new GenericANField(new Position(28, 47), "On Us");

			/* item amount */
			itemAmount = new GenericNField(new Position(48, 62), "Item Amount");

			/* ece institution item sequence number */
			eceInstitutionItemSequenceNumber = new GenericANField(new Position(
					63, 77), "ECE Institution Item Sequence Number");

			/* document type indicator */
			documentationTypeIndicator = new GenericANField(
					new Position(78, 78), "Documentation Type Indicator");

			/* return acceptance indicator */
			returnAcceptanceIndicator = new GenericANField(
					new Position(79, 79), "Return Acceptance Indicator");

			/* micr valid indicator */
			micrValidIndicator = new GenericANField(new Position(80, 80),
					"MICR Valid Indicator");

			/* bfd indicator */
			bfdIndicator = new GenericANField(new Position(81, 81),
					"BFD Indicator");

			/* check detail record addendum count */
			checkDetailRecordAddendumCount = new GenericANField(new Position(
					82, 83), "Check Detail Record Addendum Count");

			/* correction indicator */
			correctionIndicator = new GenericANField(new Position(84, 84),
					"Correction Indicator");

			/* archive type indicator */
			archiveTypeIndicator = new GenericANField(new Position(85, 85),
					"Archive Type Indicator");

			/* archive type indicator */
			origin = new GenericANField(new Position(86, 89), "Origin");

			/* archive type indicator */
			reserved = new GenericANField(new Position(90, 90), "Reserved");

			fields.add(recordType);
			fields.add(auxiliaryOnUs);
			fields.add(externalProcessingCode);
			fields.add(payorBankRoutingNumber);
			fields.add(payourBankRoutingNumberCheckDigit);
			fields.add(onUs);
			fields.add(itemAmount);
			fields.add(eceInstitutionItemSequenceNumber);
			fields.add(documentationTypeIndicator);
			fields.add(returnAcceptanceIndicator);
			fields.add(micrValidIndicator);
			fields.add(bfdIndicator);
			fields.add(checkDetailRecordAddendumCount);
			fields.add(correctionIndicator);
			fields.add(archiveTypeIndicator);
			fields.add(origin);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields3() {
		try {
			/* check detail record */
			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "25", "Record Type");

			/* aux on us */
			auxiliaryOnUs = new GenericANField(new Position(3, 17),
					"Auxiliary On Us");
			auxiliaryOnUs.setFieldData("");

			/* external processing code */
			externalProcessingCode = new GenericANField(new Position(18, 18),
					"External Processing Code");
			externalProcessingCode.setFieldData("");

			/* payor bank routing number */
			payorBankRoutingNumber = new GenericANField(new Position(19, 26),
					"Payor Bank Routing Number");
			payorBankRoutingNumber.setFieldData("");

			/* payor bank routing number check digit */
			payourBankRoutingNumberCheckDigit = new GenericANField(
					new Position(27, 27),
					"Payor Bank Routing Number Check Digit");
			payourBankRoutingNumberCheckDigit.setUsage(Usage.CONDITIONAL);
			payourBankRoutingNumberCheckDigit.setFieldData("");

			/* on us */
			onUs = new GenericANField(new Position(28, 47), "On Us");
			onUs.setFieldData("");

			/* item amount */
			itemAmount = new GenericNField(new Position(48, 62), "Item Amount");
			itemAmount.setFieldData("");

			/* ece institution item sequence number */
			eceInstitutionItemSequenceNumber = new GenericANField(new Position(
					63, 77), "ECE Institution Item Sequence Number");
			eceInstitutionItemSequenceNumber.setFieldData("000000000000000");

			/* document type indicator */
			documentationTypeIndicator = new GenericANField(
					new Position(78, 78), "Documentation Type Indicator");
			documentationTypeIndicator.setFieldData("");

			/* return acceptance indicator */
			returnAcceptanceIndicator = new GenericANField(
					new Position(79, 79), "Return Acceptance Indicator");
			returnAcceptanceIndicator.setFieldData("");

			/* micr valid indicator */
			micrValidIndicator = new GenericANField(new Position(80, 80),
					"MICR Valid Indicator");
			micrValidIndicator.setFieldData("");

			/* bfd indicator */
			bfdIndicator = new GenericANField(new Position(81, 81),
					"BFD Indicator");
			bfdIndicator.setFieldData("");

			/* check detail record addendum count */
			checkDetailRecordAddendumCount = new GenericANField(new Position(
					82, 83), "Check Detail Record Addendum Count");
			checkDetailRecordAddendumCount.setFieldData("");

			/* correction indicator */
			correctionIndicator = new GenericANField(new Position(84, 84),
					"Correction Indicator");
			correctionIndicator.setFieldData("");

			/* archive type indicator */
			archiveTypeIndicator = new GenericANField(new Position(85, 85),
					"Archive Type Indicator");
			archiveTypeIndicator.setFieldData("");

			/* archive type indicator */
			origin = new GenericANField(new Position(86, 89), "Origin");
			origin.setFieldData("");

			/* archive type indicator */
			reserved = new GenericANField(new Position(90, 90), "Reserved");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(auxiliaryOnUs);
			fields.add(externalProcessingCode);
			fields.add(payorBankRoutingNumber);
			fields.add(payourBankRoutingNumberCheckDigit);
			fields.add(onUs);
			fields.add(itemAmount);
			fields.add(eceInstitutionItemSequenceNumber);
			fields.add(documentationTypeIndicator);
			fields.add(returnAcceptanceIndicator);
			fields.add(micrValidIndicator);
			fields.add(bfdIndicator);
			fields.add(checkDetailRecordAddendumCount);
			fields.add(correctionIndicator);
			fields.add(archiveTypeIndicator);
			fields.add(origin);
			fields.add(reserved);

			this.size = 90;

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields() {
		try {
			/* check detail record */
			recordType = new GenericNField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "25", "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* aux on us */
			auxiliaryOnUs = new GenericANSField(new Position(3, 17),
					"Auxiliary On Us");
			auxiliaryOnUs.setPadder(RightJustifySpacePadder.SPACE_PADDER);
			auxiliaryOnUs.setUsage(Usage.CONDITIONAL);
			auxiliaryOnUs
					.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);
			auxiliaryOnUs.setFieldValidator(AuxOnUsValidator.INSTANCE);
			auxiliaryOnUs.setFieldData("");

			/* external processing code */
			externalProcessingCode = new GenericANSField(new Position(18, 18),
					"External Processing Code");
			externalProcessingCode
					.setPadder(RightJustifySpacePadder.SPACE_PADDER);
			externalProcessingCode.setUsage(Usage.CONDITIONAL);
			externalProcessingCode.setFieldData("");

			/* payor bank routing number */
			payorBankRoutingNumber = new GenericNField(new Position(19, 26),
					"Payor Bank Routing Number");
			payorBankRoutingNumber.setFieldData("99990004");

			/* payor bank routing number check digit */
			payourBankRoutingNumberCheckDigit = new GenericNField(new Position(
					27, 27), "Payor Bank Routing Number Check Digit");
			payourBankRoutingNumberCheckDigit.setUsage(Usage.CONDITIONAL);
			payourBankRoutingNumberCheckDigit.setFieldData("4");

			/* on us */
			onUs = new GenericANSField(new Position(28, 47), "On Us");
			onUs.setPadder(RightJustifySpacePadder.SPACE_PADDER);
			onUs.setUsage(Usage.CONDITIONAL);
			onUs.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);
			onUs.setFieldValidator(OnUsValidator.INSTANCE);
			onUs.setFieldData("12345671234567890111");

			/* item amount */
			itemAmount = new GenericNField(new Position(48, 57), "Item Amount");
			itemAmount.setFieldData("0000007200");

			/* ece institution item sequence number */
			eceInstitutionItemSequenceNumber = new GenericNBField(new Position(
					58, 72), "ECE Institution Item Sequence Number");
			eceInstitutionItemSequenceNumber
					.setPadder(RightJustifySpacePadder.SPACE_PADDER);
			eceInstitutionItemSequenceNumber.setFieldData("1");

			/* document type indicator */
			documentationTypeIndicator = new GenericANField(
					new Position(73, 73), "Documentation Type Indicator");
			documentationTypeIndicator
					.setFieldValidator(CashLetterDocumentationTypeValidator.INSTANCE);
			documentationTypeIndicator.setUsage(Usage.CONDITIONAL);
			documentationTypeIndicator
					.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);
			documentationTypeIndicator.setFieldData("");

			/* return acceptance indicator */
			returnAcceptanceIndicator = new GenericANField(
					new Position(74, 74), "Return Acceptance Indicator");
			returnAcceptanceIndicator.setUsage(Usage.CONDITIONAL);
			returnAcceptanceIndicator.setFieldData("");

			/* micr valid indicator */
			micrValidIndicator = new GenericNBField(new Position(75, 75),
					"MICR Valid Indicator");
			micrValidIndicator.setUsage(Usage.CONDITIONAL);
			micrValidIndicator.setFieldData("");

			/* bfd indicator */
			bfdIndicator = new GenericAField(new Position(76, 76),
					"BFD Indicator");
			bfdIndicator.setFieldValidator(BFDIndicatorValidator.INSTANCE);
			bfdIndicator.setValidationCriteria(ValidationCriteria.REQUIRED);
			bfdIndicator.setFieldData("Y");

			/* check detail record addendum count */
			checkDetailRecordAddendumCount = new GenericNField(new Position(77,
					78), "Check Detail Record Addendum Count");
			checkDetailRecordAddendumCount.setFieldData("01");

			/* correction indicator */
			correctionIndicator = new GenericNField(new Position(79, 79),
					"Correction Indicator");
			correctionIndicator.setUsage(Usage.CONDITIONAL);
			correctionIndicator.setFieldData("");

			/* archive type indicator */
			archiveTypeIndicator = new GenericANField(new Position(80, 80),
					"Archive Type Indicator");
			archiveTypeIndicator.setUsage(Usage.CONDITIONAL);
			archiveTypeIndicator.setFieldData("");

			fields.add(recordType);
			fields.add(auxiliaryOnUs);
			fields.add(externalProcessingCode);
			fields.add(payorBankRoutingNumber);
			fields.add(payourBankRoutingNumberCheckDigit);
			fields.add(onUs);
			fields.add(itemAmount);
			fields.add(eceInstitutionItemSequenceNumber);
			fields.add(documentationTypeIndicator);
			fields.add(returnAcceptanceIndicator);
			fields.add(micrValidIndicator);
			fields.add(bfdIndicator);
			fields.add(checkDetailRecordAddendumCount);
			fields.add(correctionIndicator);
			fields.add(archiveTypeIndicator);

		} catch (Exception e) {
			log.error(e.getMessage());
			// log.fatal(e.getMessage(), e);
		}
	}

}
